<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mintegrasi extends CI_Model {

  public function AuditTrail($log_key, $log_action, $created_by, $menu_id = null)
  {
    $db_sri = $this->load->database('Retails', TRUE);
    $log = array(
        'log_key'      => $log_key,
        'log_action'   => $log_action,
        'user_id'      => $created_by,
        'client_id'    => 4,
        'menu_code'    => 'sts',
        'created_date' => date('Y-m-d'),
        'created_time' => date('H:i:s')
      );
    $db_sri->insert('t_log', $log);
  }

  /**
   * Integrasi data sell in dari sts agar dicatat sebagai stock outlet
   * kemudian kirimkan pesan ke inboxnya outlet berdasarkan partner_id (baik OO ataupun FL akan dapat inbox)
   */
  public function StockIn($channel_id, $valid_iccid, $transaction_id, $transaction_type, $user_id = null)
  {
    $db_sts = $this->load->database('default', TRUE);
    $db_sri = $this->load->database('Retails', TRUE);

    $db_sts->select("mm.item_mapping_id, ii.iccid as serial_iccid, ii.mdn, f_get_territory_id_by_channel_id('$channel_id') as partner_id", false);
    $db_sts->join('t_mtr_item_mapping mm', 'mm.item_code = ii.item_code');
    $db_sts->where_in('ii.iccid', $valid_iccid);
    $query = $db_sts->get('t_mtr_item ii');
    $data_sts = $query->result_array();

    $db_sts->select("f_get_territory_id_by_channel_id('$channel_id') as partner_id", false);
    $query = $db_sts->get();
    $partner_id = $query->row()->partner_id;

    for ($i=0; $i < count($valid_iccid); $i++) { 
      $data_sts[$i]['partner_id'] = $partner_id;
      $data_sts[$i]['client_id']  = 4;
      $data_sts[$i]['status']     = 1;
      $data_sts[$i]['created_on'] = date('Y-m-d H:i:s');
      $data_sts[$i]['created_by'] = $transaction_type.'_'.$transaction_id;
    }

    $db_sri->insert_batch('t_mtr_item', $data_sts);

    $this->createdInbox($transaction_type, $valid_iccid, $partner_id);

    if($user_id == null ) 
      { $user_id = $this->session->userdata('username'); }
    $log = array(
        'partner_id'      => $partner_id,
        $transaction_type => $transaction_id,
        'serial_iccid'    => $valid_iccid
      );
    $this->AuditTrail('stock_in_by_'.$transaction_type, json_encode($log), $user_id);
  }

  public function StockOut($serial_iccid, $created_by, $transaction_type)
  {
    $db_sri = $this->load->database('Retails', TRUE);
    $data = array(
        'status' => -8,
        'updated_on' => date('Y-m-d H:i:s'),
        'updated_by' => $created_by
      );
    $db_sri->where_in('serial_iccid', $serial_iccid);
    $db_sri->update('t_mtr_item', $data);
    $log  = array( 'sts_trx_id'   => $created_by, 'serial_iccid' => $serial_iccid );
    $this->AuditTrail('stock_out_by_'.$transaction_type, json_encode($log), $this->session->userdata('username'));
  }

  public function createdInbox($transaction_type, $valid_iccid, $partner_id)
  {
    $db_sts = $this->load->database('default', TRUE);
    $db_sri = $this->load->database('Retails', TRUE);

    $db_sri->select('user_id, partner_id');
    $query = $db_sri->get_where('t_mtr_user', array('partner_id' => $partner_id));
    $user_sris = $query->result_array();

    $db_sts->select("ii.iccid, ii.mdn, gg.item_group_name, ii.dwh_activated", false);
    $db_sts->join('t_mtr_item_group gg', 'gg.item_group_id = ii.item_group_id');
    $db_sts->where_in('ii.iccid', $valid_iccid);
    $query = $db_sts->get("t_mtr_item ii");
    $item = $query->result_array();

    $table = '
<table cellpadding="0" cellspacing="0" border="0" class="stdtable">
  <colgroup>
      <col class="con0">
      <col class="con1">
      <col class="con0">
      <col class="con1">
      <col class="con0">
  </colgroup>
  <thead>
      <tr>
          <th class="head0">No</th>
          <th class="head1">Serial ICCID</th>
          <th class="head0">MDN</th>
          <th class="head1">Item Group</th>
          <th class="head0">Status Aktivasi</th>
      </tr>
  </thead>
  <tbody>';
    $i = 1;
    foreach ($item as $k) {
      if($k['dwh_activated'] == 1) 
        { $dwh_status = 'Sudah Aktif'; }
      else 
        { $dwh_status = 'Belum Aktif'; }
      $table .= '<tr>';
      $table .= '<td>'.$i++.'</td>';
      $table .= '<td>'.$k['iccid'].'</td>';
      $table .= '<td>'.$k['mdn'].'</td>';
      $table .= '<td>'.$k['item_group_name'].'</td>';
      $table .= '<td class="center">'.$dwh_status.'</td>';
      $table .= '<tr>';
    }
    $table .= '</tbody>
    </table>';
  
    $messege  = 'Stock Anda bertambah dari '.$transaction_type.', dengan rincian sebagai berikut: <br>';
    $messege .= $table;
    $messege .= 'Hormat kami, <br>';
    $messege .= $this->session->userdata('username');
    $messege .= '<br> ';

    foreach ($user_sris as $key) {
      $m['msg_date']                  = date('Y-m-d');
      $m['msg_from']                  = $this->session->userdata('username');
      $m['msg_from_ori_territory_id'] = $this->session->userdata('cluster_id');
      $m['msg_to']                    = $key['user_id'];
      $m['msg_to_ori_territory_id']   = $key['partner_id'];
      $m['msg_subject']               ='Penambahan Stock dari Canvaser';
      $m['msg_content']               = $messege;
      $m['status']                    = 1 ;
      $m['created_by']                = $this->session->userdata('username');
      $m['created_on']                = date('Y-m-d H:i:s');
      
      $db_sri->insert('t_msg_pm', $m);
    }

  }

  public function partner($partner_id, $partner_type)
  {
    $db_sts = $this->load->database('default', TRUE);
    $db_sri = $this->load->database('Retails', TRUE);
    if($partner_type == 'outlet') { 
      $db_sts->select("oo.outlet_id as partner_code, oo.address, oo.city, oo.province_id, oo.image_path, oo.lon, oo.lat, oo.created_by, oo.created_on, oo.updated_by, oo.updated_on");
      $db_sts->select("tt.territory_id as partner_id, tt.territory_name as partner_name, tt.parent_id as cluster_id, oo.istatus as status, xx.parent_id as region_id, ee.eload_number");
      $db_sts->join('t_mtr_outlet oo', 'oo.territory_id = tt.territory_id');
      $db_sts->join('t_mtr_outlet_eload ee', 'ee.territory_id = tt.territory_id', 'left');
      $db_sts->join('t_mtr_territory xx', 'xx.territory_id = tt.parent_id');
      $db_sts->where('oo.outlet_id', $partner_id);
    }
    else {
      $db_sts->select("aa.agent_id as partner_code, aa.agent_address, aa.agent_city as city, aa.agent_province_id as province_id, aa.image_path, aa.lon, aa.lat, aa.created_by, aa.created_on");
      $db_sts->select("tt.territory_id as partner_id, tt.territory_name as partner_name, tt.parent_id as cluster_id, aa.istatus as status, xx.parent_id as region_id");
      $db_sts->join('t_mtr_agent aa', 'aa.territory_id = tt.territory_id');
      $db_sts->join('t_mtr_territory xx', 'xx.territory_id = tt.parent_id');
      $db_sts->where('aa.agent_id', $partner_id);
    }
    $query = $db_sts->get('t_mtr_territory tt');
    $partner = $query->row_array();

    $db_sri->where('partner_id', $partner['partner_id']);
    $db_sri->from('t_mtr_partner');
    $count = $db_sri->count_all_results();

    if($count == 0) 
      { $db_sri->insert('t_mtr_partner', $partner); }
    else {
      $db_sri->where('partner_id', $partner['partner_id']);
      $db_sri->update('t_mtr_partner', $partner);
    }

    $this->AuditTrail('sts_partner_sync_'.$partner_type, json_encode($partner), $this->session->userdata('username'));
  }

  public function userSync($partner_code)
  {
    $db_sri = $this->load->database('Retails', TRUE);
    $db_sri->where('user_id', $partner_code);
    $db_sri->from('t_mtr_user');
    $user = $db_sri->count_all_results();

    $db_sts = $this->load->database('default', TRUE);

      $db_sts->select("oo.outlet_id as user_id, ow.owner_name as user_name, oo.territory_id as partner_id");
      $db_sts->select("oo.updated_by, oo.updated_on, ow.owner_phone as mobile_number, ow.owner_email as email ");
      $db_sts->join('t_mtr_territory tt', 'oo.territory_id = tt.territory_id');
      $db_sts->join('t_mtr_outlet_owner ow', 'ow.outlet_id = oo.outlet_id');
      $db_sts->where('oo.outlet_id', $partner_code);
      $query = $db_sts->get('t_mtr_outlet oo');
    
    $user_sts = $query->row_array();

    if($user != 0) {
      $db_sri->where('user_id', $partner_code);
      $db_sri->update('t_mtr_user', $user_sts);
    }
    else {
      $user_sts['user_password']             = md5('12345');
      $user_sts['user_group_id']             = 4;
      $user_sts['must_change_password']      = 1;
      $user_sts['job_title']                 = 'Outlet Owner';
      $user_sts['status']                    = 1;
      $user_sts['is_allowed_time']           = 1;
      $user_sts['last_time_change_password'] = date('Y-m-d');
      $user_sts['is_login']                  = 0;
      $user_sts['created_on']                = date('Y-m-d H:i:s');
      $user_sts['created_by']                = $this->session->userdata('username');

      $db_sri->insert('t_mtr_user', $user_sts);

    }
    // echo $this->db->last_query();
    // die();
  }

}

/* End of file mintegrasi.php */
/* Location: ./application/models/mintegrasi.php */